package nix.xstream;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.thoughtworks.xstream.XStream;

public class Demo1 {

	@Test
	public void fun1() {
		// 基本用法
		List<Province> provinceList = getProvinceList();
		XStream xstream = new XStream();
		String xmlStr = xstream.toXML(provinceList);
		System.out.println(xmlStr);
	}
	
	@Test
	public void fun2() {
		List<Province> provinceList = getProvinceList();
		XStream xstream = new XStream();
		// 别名
		xstream.alias("china", List.class);
		xstream.alias("province", Province.class);
		xstream.alias("city", City.class);
		String xmlStr = xstream.toXML(provinceList);
		System.out.println(xmlStr);
	}

	@Test
	public void fun3() {
		List<Province> provinceList = getProvinceList();
		XStream xstream = new XStream();
		// 别名
		xstream.alias("china", List.class);
		xstream.alias("province", Province.class);
		xstream.alias("city", City.class);
		// 指定javabean的属性生成为xml的元素属性
		xstream.useAttributeFor(Province.class, "name");
		String xmlStr = xstream.toXML(provinceList);
		System.out.println(xmlStr);
	}
	
	@Test
	public void fun4() {
		List<Province> provinceList = getProvinceList();
		XStream xstream = new XStream();
		// 别名
		xstream.alias("china", List.class);
		xstream.alias("province", Province.class);
		xstream.alias("city", City.class);
		// 指定javabean的属性生成为xml的元素属性
		xstream.useAttributeFor(Province.class, "name");
		// 去掉List<City> cities属性，只生成list中的元素
		xstream.addImplicitArray(Province.class, "cities");
		String xmlStr = xstream.toXML(provinceList);
		System.out.println(xmlStr);
	}
	
	@Test
	public void fun5() {
		List<Province> provinceList = getProvinceList();
		XStream xstream = new XStream();
		// 别名
		xstream.alias("china", List.class);
		xstream.alias("province", Province.class);
		xstream.alias("city", City.class);
		// 指定javabean的属性生成为xml的元素属性（默认为子元素）
		xstream.useAttributeFor(Province.class, "name");
		// 去掉Collection属性（List<City> cities）生成，只生成子元素
		xstream.addImplicitCollection(Province.class, "cities");
		// 指定javabean属性不生成xml元素
		xstream.omitField(City.class, "description");
		String xmlStr = xstream.toXML(provinceList);
		System.out.println(xmlStr);
	}

	/*
	<china>
	  <province name="北京">
	    <city>
	      <name>东城区</name>
	    </city>
	    <city>
	      <name>西城区</name>
	    </city>
	    <city>
	      <name>崇文区</name>
	    </city>
	  </province>
	  <province name="天津">
	    <city>
	      <name>和平区</name>
	    </city>
	    <city>
	      <name>河东区</name>
	    </city>
	    <city>
	      <name>河西区</name>
	    </city>
	  </province>
	</china>
	 */
	
	public List<Province> getProvinceList() {
		Province p1 = new Province();
		p1.setName("北京");
		p1.addCity(new City("东城区", "东城区"));
		p1.addCity(new City("西城区", "西城区"));
		p1.addCity(new City("崇文区", "崇文区"));
		Province p2 = new Province();
		p2.setName("天津");
		p2.addCity(new City("和平区", "和平区"));
		p2.addCity(new City("河东区", "河东区"));
		p2.addCity(new City("河西区", "河西区"));

		List<Province> provinceList = new ArrayList<Province>();
		provinceList.add(p1);
		provinceList.add(p2);

		return provinceList;
	}
}
